package cn.stylefeng.guns.modular.ws.sso.action;

import cn.stylefeng.guns.modular.ws.sso.proxy.pullSyncUserApiImpl.Exception;
import cn.stylefeng.guns.modular.ws.sso.proxy.pullSyncUserApiImpl.PullSyncUserApiImplStub;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.rest.entity.RestUser;
import cn.stylefeng.guns.sys.modular.rest.service.RestMenuService;
import cn.stylefeng.guns.sys.modular.rest.service.RestUserService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.mapper.RoleMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.sys.modular.system.model.UserDto;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.util.SpringContextHolder;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.apache.axis2.AxisFault;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.rmi.RemoteException;
import java.text.ParseException;
import java.text.SimpleDateFormat;


@Controller
@RequestMapping("/api")
public class SysnHrmController {


    @Autowired
    private UserService userService;

    @Resource
    private RestUserService restUserService;

    @Autowired
    private DeptService deptService;

    @GetMapping("/user_sync")
    @ResponseBody
    public boolean user_sync(@RequestParam("deptId") Long deptId) {
        System.out.println("fengtyfenty");
        UserDto user = new UserDto();
    /*    for(int i=0; i<3; i++) {

            System.out.println("user11:"+user.toString());
            user.setName("冯大宇");
            user.setSex("女");
            user.setEmail("123@qq.com");
            user.setAccount("fengty111");
            user.setPassword("309216366");
            user.setAvatar("gdjt");
            this.userService.addUser(user);
            user.setName("冯同宇");
            user.setSex("男");
            user.setEmail("123@qq.com");
            user.setAccount("fengty111");
            user.setPassword("309216366");
            user.setAvatar("gdjt");
            this.userService.addUser(user);
        return true ;
        }*/
        try {
            PullSyncUserApiImplStub stub = new PullSyncUserApiImplStub();
            PullSyncUserApiImplStub.GetPersonDataE perSon = new PullSyncUserApiImplStub.GetPersonDataE();
            PullSyncUserApiImplStub.GetPersonData param = new PullSyncUserApiImplStub.GetPersonData();
            PullSyncUserApiImplStub.RequestPersonModel requestPersonModelparam = new PullSyncUserApiImplStub.RequestPersonModel();
            requestPersonModelparam.setKey("11AAC64B2052093A3D77C0A419680C7F0AC066D1F790AE9B4ABD58C26ADDCA5E3A018528");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            requestPersonModelparam.setOrgId(deptId.toString()); //传入部门
            int y = 2;
            for (int i = 1; i < y; i++) {
                requestPersonModelparam.setPageNum(String.valueOf(i));
                param.setArg0(requestPersonModelparam);
                perSon.setGetPersonData(param);
                PullSyncUserApiImplStub.GetPersonDataResponseE dataResponse = stub.getPersonData(perSon);
                PullSyncUserApiImplStub.GetPersonDataResponse d = dataResponse.getGetPersonDataResponse();
                PullSyncUserApiImplStub.ResultPersons queryPersonModel = d.get_return();
                PullSyncUserApiImplStub.PersonModel[] list_ResultUserModel = queryPersonModel.getRetList();
                if(list_ResultUserModel == null || list_ResultUserModel.length == 0){
                    System.out.println("部门人员为空！"+deptId.toString());
                }else{
                for (PullSyncUserApiImplStub.PersonModel Model : list_ResultUserModel) {
                    if (!"".equals(Model.getOprId())) {
                        // 判断账号是否重复
                        RestUser theUser = restUserService.getByAccount(Model.getOprId());
                        if (theUser == null) {
                            user.setAccount(Model.getOprId());
                            user.setName(Model.getName());
                            user.setSex(Model.getSex().replace("W", "F"));
                            user.setEmail(Model.getMailAddr());
                            user.setPhone(Model.getPhone1());
                            user.setDeptId(new Long(Model.getDeptId()));  //fengty 新增部门  20201111
                            if(!"".equals(Model.getBirthdate())&& Model.getBirthdate() != null){
                                user.setBirthday(df.parse(Model.getBirthdate()));

                            }
                            user.setStatus(Model.getStatus().replace("0", "ENABLE"));
                            user.setPassword("123456");
                            user.setAvatar("kingser");

                            this.userService.addUser(user);
                        }else{
                             System.out.println("名称:"+Model.getName()+" 重复！");
                        }


                    }


                }}
                if ("1".equals(queryPersonModel.getHaseNext())) {
                    y = y + 1;
                }
                System.out.println("i:" + i + "--y:" + y);

            }

        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
            return false;
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        } catch (RemoteException e) {

            e.printStackTrace();
            return false;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return true;
    }


    @GetMapping("/org_sync")
    @ResponseBody
    public boolean org_sync() {
        //http://sso.kingser.com:90/esc-ws/services/pullSyncUserApiImpl
        System.out.println("fengtyfenty");
       Dept dept = new Dept() ;
  /*       dept.setPid(new Long("27"));
        dept.setPids("[21],[24],[27],");
        dept.setDescription("秘书3部");
        dept.setFullName("秘书3部");
        dept.setSort(13);
        dept.setSimpleName("秘书3部");
        this.deptService.addDeptAPI(dept);
        System.out.println("12321123");

        if(1==1){
            return true ;
        }*/
        try {
            PullSyncUserApiImplStub stub = new PullSyncUserApiImplStub();
            PullSyncUserApiImplStub.GetOrgDataE  orgDataE = new PullSyncUserApiImplStub.GetOrgDataE() ;
            PullSyncUserApiImplStub.GetOrgData  orgData = new  PullSyncUserApiImplStub.GetOrgData() ;
            PullSyncUserApiImplStub.RequestOrgModel orgModel = new PullSyncUserApiImplStub.RequestOrgModel() ;
            orgModel.setKey("11AAC64B2052093A3D77C0A419680C7F0AC066D1F790AE9B4ABD58C26ADDCA5E3A018528");
            //orgModel.setDeptFlag("0");  //部门1/分部0
            orgModel.setCompanyId("110001");
            int y= 2 ;
            for (int i=1;i<y;i++){
                orgModel.setPageNum(String.valueOf(i));
                orgData.setArg0(orgModel);
                orgDataE.setGetOrgData(orgData);

                PullSyncUserApiImplStub.GetOrgDataResponseE orgDataResponseE = stub.getOrgData(orgDataE) ;
                PullSyncUserApiImplStub.GetOrgDataResponse orgDataResponse = orgDataResponseE.getGetOrgDataResponse();
                PullSyncUserApiImplStub.ResultOrgs resultOrgs = orgDataResponse.get_return() ;
                PullSyncUserApiImplStub.OrgModel[] orgModels = resultOrgs.getRetList() ;
                for(PullSyncUserApiImplStub.OrgModel Model : orgModels){

                    String[] str = Model.getOrgPathId().substring(1).split("/") ;
                    String Pids = "" ;
                    for(int ii=0;ii<str.length-1;ii++){
                        Pids += "[" + str[ii] + "],";
                    }
                    dept.setDeptId(new Long(Model.getDeptId()));
                    dept.setPid(new Long(Model.getParentDeptId()));
                    dept.setPids(Pids);
                    dept.setDescription(Model.getDescr());
                    dept.setFullName(Model.getDeptName());
                    dept.setSort(Integer.parseInt(Model.getDeptId()));
                    dept.setSimpleName(Model.getDescrShort());
                    this.deptService.addDeptAPI(dept);
                    user_sync(dept.getDeptId());//同步人员信息
                   /*
                    if(Model.getTreeLevelNum()==null){

                            System.out.println("---------------------Model.getTreeLevelNum():"+Model.getTreeLevelNum());
                            dept.setDeptId(new Long(Model.getDeptId()));
                            dept.setPid(new Long(Model.getParentDeptId()));

                            dept.setDescription(Model.getDescr());
                            dept.setFullName(Model.getDeptName());
                            dept.setSort(Integer.parseInt(Model.getParentDeptId()));
                            dept.setSimpleName(Model.getDeptName());
                            this.deptService.addDept(dept);



                    }else if(Integer.parseInt(Model.getTreeLevelNum()) == 1){
                        System.out.println("========================Model.getTreeLevelNum():"+Model.getTreeLevelNum());
                        dept.setDeptId(new Long(Model.getDeptId()));
                        dept.setPid(new Long(Model.getParentDeptId()));
                        dept.setDescription(Model.getDescr());
                        dept.setFullName(Model.getDeptName());
                        dept.setSort(Integer.parseInt(Model.getParentDeptId()));
                        dept.setSimpleName(Model.getDeptName());
                        this.deptService.addDept(dept);
                        System.out.println("++++++++");

                    }*/
                    //for (int x=1;x<5;x++){
                        /*    if(Integer.parseInt(Model.getTreeLevelNum()) == 1&&!"".equals(Model.getTreeLevelNum())&&Model.getTreeLevelNum()!=null){
                                System.out.println("Model.getTreeLevelNum():"+Model.getTreeLevelNum());
                                dept.setDeptId(new Long(Model.getDeptId()));
                                dept.setPid(new Long(Model.getParentDeptId()));
                                dept.setDescription(Model.getDescr());
                                dept.setFullName(Model.getDeptName());
                                dept.setSort(Integer.parseInt(Model.getParentDeptId()));
                                dept.setSimpleName(Model.getDeptName());
                                this.deptService.addDept(dept);
                            }*/
                   // }




                    System.out.println("名称:"+Model.getOrgPathId());
                }
                if("1".equals(resultOrgs.getHaseNext())){
                    y = y + 1 ;
                }

            }

            return true ;
        } catch (AxisFault axisFault) {
            axisFault.printStackTrace();
            return false;
        } catch (Exception e) {

            e.printStackTrace();
            return false;
        } catch (RemoteException e) {

            e.printStackTrace();
            return false;
        }

    }
}
